package app.android.com.wuandroidapp.test.view.definedview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import app.android.com.utilscode.util.RoundUtil;

/**
 * Created by admin on 2017/6/28.
 */

public class CirCleSView extends View {

    private int color = Color.RED;
    private int color1 = Color.BLUE;
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint mGesturePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint paint1 = new Paint();
    private Point[][] points = new Point[3][3];


    private final Path mPath = new Path();
    // 选中的点
    private List<Point> sPoints = new ArrayList<Point>();

    public CirCleSView(Context context) {
        super(context);
        init();
    }

    public CirCleSView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CirCleSView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (!isOne) {
            initCircle();
        }
        initDraw(canvas);
        if(sPoints.size()>0) {
            
            canvas.drawLine(sPoints.get(0).x, sPoints.get(0).y,move_exs,move_eys,paint);
        }
    }

    float move_exs;
    float move_eys;
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        float ex = event.getX();
        float ey = event.getY();
        Point p = null;
        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN: {
                p = checkSelectPoint(ex, ey);

                break;
            }
            case MotionEvent.ACTION_MOVE: {
                //两点连成直线
                 move_exs = event.getX();
                 move_eys = event.getY();
                break;
            }
            case MotionEvent.ACTION_UP: {
                break;
            }


        }

        if (p != null) {
            p.state = Point.STATE_CHECK;
            addPoint(p);

        }
        this.postInvalidate();
        return true;
    }

    /**
     * 添加一个点
     *
     * @param point
     */
    private void addPoint(Point point) {
        this.sPoints.add(point);
    }

    /**
     * 检查
     *
     * @param x
     * @param y
     * @return
     */
    private Point checkSelectPoint(float x, float y) {
        for (int i = 0; i < points.length; i++) {
            for (int j = 0; j < points[i].length; j++) {
                Point p = points[i][j];
                if (RoundUtil.checkInRound(p.x, p.y, radius, (int) x, (int) y)) {
                    return p;
                }
            }
        }
        return null;
    }


    boolean isOne = false;




    public void initDraw(Canvas canvas) {
        for (int i = 0; i < points.length; i++) {
            for (int j = 0; j < points[i].length; j++) {
                Point p = points[i][j];
                if (p.state == Point.STATE_CHECK) {
                    canvas.drawCircle(p.x, p.y, radius, paint1);
                } else if (p.state == Point.STATE_CHECK_ERROR) {

                } else {
                    canvas.drawCircle(p.x, p.y, radius, paint);
                }
            }
        }
    }



    int radius;

    public void initCircle() {
        int width = getWidth() / 6;
        int height = getWidth() / 6;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                radius = Math.min(width, height) / 2;
                //      canvas.drawCircle(width + (width * 2 * j), height + (width * 2 * i), radius, paint);
                points[i][j] = new Point(width + (width * 2 * j), height + (width * 2 * i));
            }
        }
        isOne = true;
    }

    public void init() {
        paint.setColor(color);
        paint1.setColor(color1);
        mGesturePaint.setAntiAlias(true);
        mGesturePaint.setStyle(Paint.Style.STROKE);
        mGesturePaint.setStrokeWidth(5);
        mGesturePaint.setColor(color);
    }
}
